//
//  JSPayPasswordView.m
//  NiubilityProject
//
//  Created by Apple on 2018/3/27.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSPayPasswordView.h"
#import "JSPassTextField.h"

static NSInteger const kDotsNumber = 6;
static CGFloat const kDotWith_height = 10;
@interface JSPayPasswordView ()

@property (nonatomic, strong) NSMutableArray *passwordDotsArray;
@end

@implementation JSPayPasswordView

- (instancetype)init{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    [self addSubview:self.passTextField];
    [self.passTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    UIView *temp = nil;
    for (int i = 0; i < kDotsNumber; i ++)
    {
        UIView *childView = [UIView new];
        childView.backgroundColor = [UIColor clearColor];
        UIView *dotView = [UIView new];
        dotView.backgroundColor = [UIColor blackColor];
        [self.passTextField addSubview:childView];
        dotView.layer.cornerRadius = kDotWith_height/2.0;
        dotView.hidden = YES;
        [self.passTextField addSubview:dotView];
        [self.passwordDotsArray addObject:dotView];
        UIView *line = [UIView new];
        line.backgroundColor = [UIColor lightGrayColor];
        [self.passTextField addSubview:line];
        if (temp) {
            [childView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(temp.mas_right);
                make.width.equalTo(temp);
                make.top.bottom.equalTo(temp);
            }];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.right.bottom.equalTo(childView);
                make.width.mas_equalTo(0.5);
            }];
            temp = childView;
        }else{
            [childView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.bottom.equalTo(self.passTextField);
                make.width.equalTo(childView.mas_height);
            }];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.right.bottom.equalTo(childView);
                make.width.mas_equalTo(0.5);
            }];
            
            temp = childView;
        }
        
        [dotView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(childView);
            make.size.mas_equalTo(CGSizeMake(kDotWith_height, kDotWith_height));
        }];
    }
    
}

- (JSPassTextField *)passTextField{
    if (!_passTextField){
        _passTextField = [[JSPassTextField alloc] init];
        _passTextField.delegate = (id)self;
        _passTextField.backgroundColor = [UIColor whiteColor];
        _passTextField.textColor = [UIColor clearColor];
        _passTextField.tintColor = [UIColor clearColor];
        _passTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _passTextField.layer.borderWidth = 0.5;
        _passTextField.keyboardType = UIKeyboardTypeNumberPad;
        _passTextField.secureTextEntry = YES;
    }
    [_passTextField addTarget:self action:@selector(passwordFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    return _passTextField;
}

// 将所有的假密码点设置为隐藏状态
- (void)setDotsViewHidden{
    for (UIView *view in _passwordDotsArray){
        [view setHidden:YES];
    }
}
- (void)passwordFieldDidChange:(UITextField *)field{
    [self setDotsViewHidden];
    
    for (int i = 0; i < _passTextField.text.length; i ++){
        if (_passwordDotsArray.count > i ) {
            UIView *dotView = _passwordDotsArray[i];
            [dotView setHidden:NO];
        }
    }
    
    if (_passTextField.text.length == kDotsNumber){
        NSLog(@"密码 %@",_passTextField.text);
        [self.passTextField resignFirstResponder];
        if ([_delegate respondsToSelector:@selector(passwordView:didFinishInputPayPassword:)]) {
            [_delegate passwordView:self didFinishInputPayPassword:_passTextField.text];
        }
    }
}
#pragma mark == UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //删除键
    if (string.length == 0){
        return YES;
    }
    
    if (_passTextField.text.length >= kDotsNumber){
        return NO;
    }
    return YES;
}

-(NSMutableArray *)passwordDotsArray{
    if (!_passwordDotsArray) {
        _passwordDotsArray = @[].mutableCopy;
    }
    return _passwordDotsArray;
}

-(void)show{
    [self.passTextField becomeFirstResponder];
}
@end
